package com.time.workshop.ui;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.time.workshop.API;
import com.time.workshop.BaseConstant;
import com.time.workshop.R;
import com.time.workshop.SuperActivity;
import com.time.workshop.bean.BaseResponse;
import com.time.workshop.bean.RequestOrder;
import com.time.workshop.bean.UserInfo;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.utils.CommonUtils;
import com.time.workshop.utils.Log;
import com.time.workshop.utils.NewToast;
import com.time.workshop.utils.PreferenceUtil;
/**
 * 意见反馈页面
 * @author wangfeng
 *
 */
public class FeedbackActivity extends SuperActivity{

	@V
	private Button feedback_order_titlebar_left_btn;
	@V
	private EditText feedback_order_remarksEditText;
	@V
	private TextView feedback_order_commitOrderTextView;
	private String feedback;
	@Override
	protected void onCreate(Bundle bundle) {
		// TODO Auto-generated method stub
		super.onCreate(bundle);
		setContentView(R.layout.activity_feedback);
		Injector.getInstance().inject(this);
		feedback_order_titlebar_left_btn.setOnClickListener(this);
		feedback_order_commitOrderTextView.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		super.onClick(view);
		switch (view.getId()) {
		case R.id.feedback_order_titlebar_left_btn:
			back();
			break;
		case R.id.feedback_order_commitOrderTextView:
			feedback = feedback_order_remarksEditText.getText().toString().trim();
			if ("".equals(CommonUtils.isNull(feedback))) {
				NewToast.makeText(getApplicationContext(), getResources().getString(R.string.toast_feedback));
				return;
			}
			if ("".equals(CommonUtils.isNull(PreferenceUtil.getString(
					FeedbackActivity.this, BaseConstant.PREF_TOKE_STRING)))
					|| "".equals(CommonUtils.isNull(PreferenceUtil.getString(
							FeedbackActivity.this,
							BaseConstant.PREF_USERNAME_STRING)))
					|| "".equals(CommonUtils.isNull(PreferenceUtil.getString(
							FeedbackActivity.this,
							BaseConstant.PREF_PASSWORD_STRING)))) {
				Intent in = new Intent(FeedbackActivity.this,
						LoginActivity.class);
				startActivity(in, true);
				return;
			}
			if (!CommonUtils.isNetworkConnected(getApplicationContext())) {
				NewToast.makeText(getApplicationContext(), getResources().getString(R.string.M000));
				return;
			}
			submit();
			break;
		default:
			break;
		}
	}

	private void submit(){
		HttpUtils http = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("QUESTION", feedback);
		params.addBodyParameter("token", PreferenceUtil.getString(FeedbackActivity.this,
				BaseConstant.PREF_TOKE_STRING));
		showDialog(null);
		http.send(HttpMethod.POST, API.feedback, params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					JSONObject object = new JSONObject(responseInfo.result);
					boolean success = object.optBoolean("success");
					String msg = object.optString("message");
					int code = object.optInt("resCode");
					if (success) {
						NewToast.makeText(getApplicationContext(), msg);
						dismissLoadingDialog();
						back();
					}else {
						if (1 == code) {
							doLoginRequest(PreferenceUtil.getString(
									FeedbackActivity.this,
				BaseConstant.PREF_USERNAME_STRING),PreferenceUtil.getString(
						FeedbackActivity.this,
				BaseConstant.PREF_PASSWORD_STRING));
						}
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				// TODO Auto-generated method stub
				NewToast.makeText(getApplicationContext(), msg);
				dismissLoadingDialog();
			}
		});
	}
	
	/** 返回数据总体的实体类 */
	private class Response extends BaseResponse<UserInfo>{};
	
	private static Response mUserInfo;
	
	private static RequestOrder requestOrder;
	/**token过期后再次登录 */
	private void doLoginRequest(String pho,final String pwd) {
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("username", pho);
		params.addQueryStringParameter("password", pwd);
		
		HttpUtils httpUtils = new HttpUtils();
		
		httpUtils.send(HttpMethod.GET, API.login,params,new RequestCallBack<String>() {

			@Override 
			public void onSuccess(ResponseInfo<String> responseInfo) {
				Log.d("onSuccess==", responseInfo.result+"");
				
				try {
					JSONObject jsonObject = new JSONObject(responseInfo.result);
					Gson gson = new Gson();
					
					mUserInfo = gson.fromJson(jsonObject.toString(), Response.class);
					
					Log.d("mUserInfo", mUserInfo.getResult()+"");
					
					if(mUserInfo.isSuccess()){
						
						//保存登录返回的Token
						PreferenceUtil.putString(FeedbackActivity.this, BaseConstant.PREF_TOKE_STRING, mUserInfo.getResult().getTOKEN()+"");
						submit();
					}else {
						Intent in = new Intent(FeedbackActivity.this,
								LoginActivity.class);
						startActivity(in, true);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				Log.d("onFailure==", msg+"");
				Intent in = new Intent(FeedbackActivity.this,
						LoginActivity.class);
				startActivity(in, true);
			}
			
		});
	}
	
}
